﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using NetCoreCMS.Web.Areas.WebApi.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class User_RePasswordController : BaseController
    {
        public ISms_templateService _sms_template { get; set; }
        public IUser_codeService _user_code { get; set; }
        protected Userconfig userConfig;
        protected Sysconfig sysConfig;
        private AliSMS sendSMS;
        [HttpGet]
        public IActionResult Get(string username, string code)
        {
            //检查用户名
            if (string.IsNullOrEmpty(username))
            {
                return Ok(new { status = 0, msg = "温馨提示：用户名不可为空！" });
            }
            //校检验证码
            string result = CheckCode.verify_code(code, HttpContext);
            if (result != "success")
            {
                return Ok(result);
            }
            //检查用户信息
            Users model = _users.GetModel(username);
            if (model == null)
            {
                return Ok(new { status = 0, msg = "温馨提示：您输入的用户名不存在！" });
            }
            userConfig = Common.XMLConfig.Userconfig.loadConfig(_env.ContentRootPath);
            sysConfig = Common.XMLConfig.Sysconfig.loadConfig(_env.ContentRootPath);
            sendSMS = new AliSMS(sysConfig.smsusername, sysConfig.smspassword, sysConfig.smssignname);
            //发送取回密码的短信
            #region 发送短信==================
            if (string.IsNullOrEmpty(model.mobile))
            {
                return Ok(new { status = 0, msg = "温馨提示：无法取回密码！" });
            }
            Sms_template smsModel = _sms_template.GetModel("usercode"); //取得短信内容
            if (smsModel == null)
            {
                return Ok(new { status = 0, msg = "温馨提示：发送失败，短信模板不存在，请联系管理员！" });
            }
            string strcode = Utils.Number(4); //随机验证码
                                              //检查是否重复提交
            User_code codeModel;
            codeModel = _user_code.GetModel(username, CMSEnums.CodeEnum.RegVerify.ToString(), "d");
            if (codeModel == null)
            {
                codeModel = new User_code();
                //写入数据库
                codeModel.user_id = model.id;
                codeModel.user_name = model.user_name;
                codeModel.type = CMSEnums.CodeEnum.Password.ToString();
                codeModel.str_code = strcode;
                codeModel.eff_time = DateTime.Now.AddMinutes(userConfig.regsmsexpired);
                codeModel.add_time = DateTime.Now;
                _user_code.Add(codeModel);
            }
            //替换标签
            string msgContent = smsModel.content;
            msgContent = msgContent.Replace("{webname}", sysConfig.webname);
            msgContent = msgContent.Replace("{weburl}", sysConfig.weburl);
            msgContent = msgContent.Replace("{webtel}", sysConfig.webtel);
            msgContent = msgContent.Replace("{code}", codeModel.str_code);
            msgContent = msgContent.Replace("{valid}", userConfig.regsmsexpired.ToString());
            //发送短信
            string tipMsg = string.Empty;

            bool result1 = sendSMS.Send(model.mobile, smsModel.code, msgContent);
            if (!result1)
            {
                return Ok(new { status = 0, msg = "温馨提示：发送失败，" + tipMsg + "！" });
            }
            return Ok(new { status = 1, msg = "手机验证码发送成功" });
            #endregion
        }
    }
}